package cn.iocoder.yudao.module.bill.service.bill;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.bill.controller.admin.bill.vo.*;
import cn.iocoder.yudao.module.bill.dal.dataobject.bill.BillDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.bill.dal.mysql.bill.BillMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.bill.enums.ErrorCodeConstants.*;

/**
 * 账单信息 Service 实现类
 *
 * @author mmciel
 */
@Service
@Validated
public class BillServiceImpl implements BillService {

    @Resource
    private BillMapper billMapper;

    @Override
    public Long createBill(BillSaveReqVO createReqVO) {
        // 插入
        BillDO bill = BeanUtils.toBean(createReqVO, BillDO.class);
        billMapper.insert(bill);
        // 返回
        return bill.getId();
    }

    @Override
    public void updateBill(BillSaveReqVO updateReqVO) {
        // 校验存在
        validateBillExists(updateReqVO.getId());
        // 更新
        BillDO updateObj = BeanUtils.toBean(updateReqVO, BillDO.class);
        billMapper.updateById(updateObj);
    }

    @Override
    public void deleteBill(Long id) {
        // 校验存在
        validateBillExists(id);
        // 删除
        billMapper.deleteById(id);
    }

    private void validateBillExists(Long id) {
        if (billMapper.selectById(id) == null) {
            throw exception(BILL_NOT_EXISTS);
        }
    }

    @Override
    public BillDO getBill(Long id) {
        return billMapper.selectById(id);
    }

    @Override
    public PageResult<BillDO> getBillPage(BillPageReqVO pageReqVO) {
        return billMapper.selectPage(pageReqVO);
    }

    @Override
    public BillImportVO importList(List<BillImportVO> list) {
        list.forEach(System.out::println);
        List<BillDO> beans = BeanUtils.toBean(list, BillDO.class);
        billMapper.insertBatch(beans,200);
        return null;
    }

    @Override
    public void deleteBatchBill(List<Long> ids) {
        billMapper.deleteByIds(ids);
    }

    @Override
    public void updateBatchBill(List<Long> ids, BillSaveReqVO updateReqVO) {
        List<BillDO> list = ids.stream().map(t -> {
            BillDO bean = BeanUtils.toBean(updateReqVO, BillDO.class);
            bean.setId(t);
            return bean;
        }).toList();
        billMapper.updateById(list,100);
    }

}